import AsyncStorage from '@react-native-community/async-storage';
var Ugame = {
}
Ugame.base_img={};
Ugame.site={};
Ugame.gmlist={};
Ugame.info={
    ExpireTime: null,
    SeverTime: null,
    Token: null,
}
Ugame.clearInfo=function(){
    Ugame.info={
        ExpireTime: null,
        SeverTime: null,
        Token: null,
    }
}
Ugame.constDataSource=[];
Ugame.deleteConstDataSource=function(number){
    let temp = [];
    for (let i=0;i<Ugame.constDataSource.length;i++){
        if(Ugame.constDataSource[i].number==number){
            continue;
        }
        temp.push(Ugame.constDataSource[i]);
    }
    Ugame.constDataSource = temp;
}
Ugame.getNumberDataSource=function(number){
    if(Ugame.constDataSource.length==0) return;
    for(let i=0;i<Ugame.constDataSource.length;i++){
        let item = Ugame.constDataSource[i].data;
        for (let j=0;j<item.length;j++){
            if(item[j].number==number){
                return item[j];
                break;
            }
        }
    }
}

Ugame.setExpireTime=function (Expire) {
    let timestamp = parseInt(new Date().getTime()/1000);    // 当前时间戳
    return timestamp + Expire;
}
Ugame.checkExpire=function () {
    return parseInt(new Date().getTime()/1000) >= Ugame.info.ExpireTime;
}
Ugame.setUserStorage=function(callback){
    AsyncStorage.setItem('info',JSON.stringify(this.info)).then(()=>{
        callback();
    });
}
Ugame.loadUserStorgae=function(){
    AsyncStorage.getItem('info').then((result)=>{
        if(!!result){
            Ugame.info = JSON.parse(result);
        }
    })
}
Ugame.setUserData=function(data,callback){
    Ugame.info.Token=data.Token;
    Ugame.info.SeverTime = data.SeverTime;
    Ugame.info.ExpireTime = Ugame.setExpireTime(data.Expire);
    Ugame.setUserStorage(callback);
}
function timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
}

Ugame.isLogin=function(){
    return !!this.info.Token && this.info.ExpireTime > parseInt(new Date().getTime()/1000);
}
Ugame.clearUserStorgae=function(callback){
    Ugame.info={
        ExpireTime: null,
        SeverTime: null,
        Token: null,
    }
    Ugame.setUserStorage(callback);
}

/*---------------------------大乐透-------------------------*/
Ugame.DLTBox=[];//大乐透号码蓝
Ugame.getDLTBoxCount=function(){
    return Ugame.DLTBox.length;
}
Ugame.addDLTBox=function(obj){
    Ugame.DLTBox.push(obj);
    return Ugame.getDLTBoxCount();
}
Ugame.delDLTBox=function(index){
    Ugame.DLTBox.splice(index,1);
    return Ugame.DLTBox;    
}
Ugame.clearDLTBox=function(){
    Ugame.DLTBox=[];
    return Ugame.DLTBox;
}

Ugame.DLTSelectList=[];
Ugame.addDLTSelectList=function(array){
    for(let i =0;i<array.length;i++){
        Ugame.DLTSelectList.push(array[i]);
    }
    return Ugame.DLTSelectList;
}
Ugame.delDLTSelectList=function(index){
    Ugame.DLTSelectList.splice(index,1);
    return Ugame.DLTSelectList;    
}
Ugame.clearDLTSelectList=function(){
    Ugame.DLTSelectList=[];
    return  Ugame.DLTSelectList;
}
/*---------------------------------------------------------*/

Ugame.loadUserStorgae();
module.exports = Ugame